package com.swipemaster.opengl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;

import com.swipemaster.levels.Level;

public class GameRenderer implements GLSurfaceView.Renderer
{
	private OpenGLView openGLView;
	private Level level;

	public GameRenderer(OpenGLView openGLView, Level level)
	{
		this.openGLView = openGLView;
		this.level = level;
	}

	public void onDrawFrame(GL10 gl)
	{
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		level.draw(gl);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height)
	{
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glOrthof(0, width, height, 0, -1, 1);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config)
	{
		gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
	}

	/**
	 * Will be called periodically. Here we can request to render on a view.
	 */
	public void tick()
	{
		openGLView.requestRender();
	}
	
}
